package com.example.design.phone;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author nlsm
 */
@Slf4j
public class ClassMapFactory {
    private static final Map<Class<?>, Compiler<?>> MAP = new HashMap<>();

    public static Compiler<?> getMap(Class<?> clazz){
        if (clazz.isInterface()) {
            throw new UnsupportedOperationException("desensitization is interface, what is expected is an implementation class !");
        }
        return MAP.computeIfAbsent(clazz, k -> {
            try {
                return (Compiler<?>) clazz.getDeclaredConstructor().newInstance();
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                     NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        });
    }

    public static void print(){
        MAP.forEach((k, v) -> {
            log.info("主键={} 值={}", k, v);
        });
    }
}
